#####################################################
# Seurat Analysis
####################################################
# Read 10xData into Seurat Object
Data_GFP24hpf<-Read10X(Data = "$Path/filtered_feature_bc_matrix/")
Data_GFP24hpf <- CreateSeuratObject(counts = Data_GFP24hpf, project = "tNCCGFP", min.cells = 3, min.features = 200)
Data_GFP24hpf[["percent.mt"]] <- PercentageFeatureSet(Data_GFP24hpf, pattern = "^mt-")
# Filter Cells
Keep <- row.names(Data_GFP24hpf@meta.Data_GFP24hpf[which(Data_GFP24hpf@meta.Data_GFP24hpf$percent.mt <= 6 & 
                                                           Data_GFP24hpf@meta.Data_GFP24hpf$nCount_RNA <16000 & 
                                                           Data_GFP24hpf@meta.Data_GFP24hpf$nCount_RNA >=5000),])
Data_GFP24hpf <- Data_GFP24hpf[,Keep,]
#SCTransform
Data_GFP24hpf <- SCTransform(Data_GFP24hpf)
#PCA
Data_GFP24hpf <- RunPCA(Data_GFP24hpf,features= VariableFeatures(Data_GFP24hpf),npcs = 100)
# Cluster...
Data_GFP24hpf<- FindNeighbors(Data_GFP24hpf,dims= 1:15)
Data_GFP24hpf <- FindClusters(Data_GFP24hpf, resolution = .6)
# UMAP
Data_GFP24hpf<-RunUMAP(Data_GFP24hpf,dims = 1:15,n.components = 2)
# Differential Expression
DEWilcox<-FindAllMarkers(Data_GFP24hpf,logfc.threshold = 0.5,only.pos = T,min.pct = .25)

#####################################################
# Integration Analysis
####################################################
# Datasets are seurat objects from different datasets (see main text for discussion)
# Data_GFP24hpf is current study data
# Data_GFP48hpf is 48hpf data from Howard et al. 2020
# Data_GFP68hpf is 68hpf data from Howard et al. 2020
#Integrate list
IntegrateHoward.List<-list(Data_GFP24hpf,Data_GFP48hpf,Data_GFP68hpf)
#Normalize
IntegrateHoward.List <- lapply(X = IntegrateHoward.List, FUN = function(x) {
  x <- NormalizeData(x)
  x <- FindVariableFeatures(x, selection.method = "vst", nfeatures = 2000)
})
#
featuresHoward <- SelectIntegrationFeatures(object.list = IntegrateHoward.List)
AnchorsHoward <- FindIntegrationAnchors(object.list = IntegrateHoward.List, anchor.features = features)
#
Howard_Integrated <- IntegrateData(anchorset = AnchorsHoward)
#
DefaultAssay(Howard_Integrated) <- "integrated"
#
Howard_Integrated <- ScaleData(Howard_Integrated, verbose = FALSE)
Howard_Integrated <- RunPCA(Howard_Integrated, npcs = 100, verbose = FALSE)
#
Howard_Integrated <- RunUMAP(Howard_Integrated, reduction = "pca", dims = 1:30)
#
DEWilcox<-FindMarkers(Howard_Integrated,ident.1 = "Xanthoblast",ident.2 = "Xanthophore",assay = "RNA")
# Same Process done on Wagner et al. 2018 data. Only difference is using 100PCAs for UMAP

#####################################################
# Velocyto
####################################################
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # The Following python implementation is called from bash  # # # # 
# #
# #    velocyto run10x /path/to/10xData
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

# In R....
Data.velo<-ReadVelocity("/Path/To/LoomFile")
Data.velo<-as.Seurat(x = Data.velo)
#filter
Data.velo<-Data.velo[,"FilteredCellsFromAbove",]
#
Data.velo<-SCTransform(Data.velo,assay="spliced")
#
Data.velo <- RunVelocity(object = Data.velo, deltaT = 1, kCells = 25, fit.quantile = 0.02)
#
show.velocity.on.embedding.cor(emb = Embeddings(object = Data.velo, reduction = "umap"), vel = Tool(object = Data.velo,slot = "RunVelocity"), n = 200, scale = "sqrt", cell.colors = ac(x = cell.colors, alpha = 1), cex = 0.8, arrow.scale = 3, show.grid.flow = TRUE, min.grid.cell.mass = 0.5, grid.n = 40, arrow.lwd = 1, do.par = FALSE, cell.border.alpha = 0.1)

